package com.moli.iov.admin.web.controller;

import com.moli.iov.admin.model.vo.req.BaseVo;
import com.moli.iov.admin.model.vo.req.VehicleGpsParamQueryVo;
import com.moli.iov.admin.model.vo.req.VehicleGpsQueryVo;
import com.moli.iov.admin.model.vo.res.VehicleGpsVo;
import com.moli.iov.base.enums.AuthorizedEnum;
import com.moli.iov.base.model.po.VehicleBase;
import com.moli.iov.base.remote.IVehicleBaseRemoteService;
import com.moli.iov.base.service.IVehicleBaseService;
import com.moli.iov.enums.UserTypeEnum;
import com.moli.iov.his.dto.VehicleTrajectoryReqDTO;
import com.moli.iov.his.dto.VehicleTrajectoryResDTO;

import com.moli.iov.his.enums.VehicleQueryType;
import com.moli.iov.his.remote.HistoryRemoteService;

import com.moli.iov.his.remote.IVehicleTrainTrajectoryRemoteService;
import com.moli.iov.his.service.IVehicleTrainTrajectoryService;
import com.moli.iov.rest.RestResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Monitor vehicle train trajectory stat.
 *
 * @author OAK
 * @version 3.0
 * @since 2019/10/15 13:54 PM.
 */
@Slf4j
@RestController
@RequestMapping(value = "/monitor")
public class VehicleGpsController extends BaseController {

    @Reference(group = "${service.group}", check = false, timeout = 20000)
    private HistoryRemoteService historyRemoteService;

    @Reference(group = "${service.group}", check = false)
    private IVehicleBaseRemoteService baseRemoteService;

    @Reference(group = "${service.group}", check = false, timeout = 20000)
    private IVehicleTrainTrajectoryRemoteService vehicleTrainTrajectoryRemoteService;

    @PostMapping("/track")
    public RestResponse<List<VehicleTrajectoryResDTO>> queryVehicleGpsByVinAndTime(@RequestBody @Validated VehicleGpsParamQueryVo vehicleGpsParamQueryVo) {
        assembleUserInfo(vehicleGpsParamQueryVo);
        try {
            log.info("----------queryVehicleGpsByVinAndTime请求入参：" + vehicleGpsParamQueryVo.toString());
            assembleUserInfo(vehicleGpsParamQueryVo);
            VehicleBase vehicleBase = CheckVehicleBase(vehicleGpsParamQueryVo);
            if (null == vehicleBase) {
                return RestResponse.failure("查无车辆");
            } else {
                VehicleTrajectoryReqDTO dto = new VehicleTrajectoryReqDTO();
                dto.setEndTime(vehicleGpsParamQueryVo.getEndTime());
                dto.setNum(vehicleGpsParamQueryVo.getNum());
                dto.setNumType(vehicleGpsParamQueryVo.getNumType());
                dto.setStartTime(vehicleGpsParamQueryVo.getStartTime());
                long start = System.currentTimeMillis();
                RestResponse<List<VehicleTrajectoryResDTO>> res = historyRemoteService.listVehicleTrajectory(dto);
                log.info("****查询轨迹时长：{}", (System.currentTimeMillis() - start));
                List<VehicleTrajectoryResDTO> list = null;
                if (res != null && res.getData() != null) {
                    list = res.getData();
                    for (VehicleTrajectoryResDTO resDTO : list) {
                        resDTO.setVin(vehicleBase.getVin());
                        resDTO.setLicense(vehicleBase.getLicense());
                    }
                    return RestResponse.success("查询成功", list);
                } else {
                    return RestResponse.success("查询成功", list);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.failure("查询失败");
        }

    }

    /**
     * According vin to query vehicle trajectory.
     *
     * @param vehicleGpsQueryVo Vehicle trajectory model.
     * @return Whether success.
     */
    @PostMapping("/trajectory")
    public RestResponse<List<String>> queryVehicleTrajectoryByVin(@RequestBody @Validated VehicleGpsQueryVo vehicleGpsQueryVo) {
        assembleUserInfo(vehicleGpsQueryVo);
        VehicleBase vehicleBase = CheckVehicleBase(vehicleGpsQueryVo);
        if (null == vehicleBase) {
            return RestResponse.failure("查无车辆");
        } else {
            return vehicleTrainTrajectoryRemoteService.queryVehicleTrajectoriesByVin(vehicleBase.getVin());
        }
    }

    /**
     * 校验数据
     *
     * @param vo the vo
     * @return the vehicle base
     */
    public VehicleBase CheckVehicleBase(VehicleGpsQueryVo vo) {
        if (VehicleQueryType.LICENSE.getCode().equals(vo.getNumType())) {
            try {
                RestResponse<VehicleBase> vehicleBase = baseRemoteService.queryVehicleBaseByLicense(vo.getNum());
                //企业用户排除未授权的车辆
                if (RestResponse.isSuccess(vehicleBase)) {
                    if (vo.getSysUserType().intValue() == UserTypeEnum.COMPANY.getFlag()
                            && vehicleBase.getData().getAuthorizedStatus().intValue() == AuthorizedEnum.UN_AUTHORIZED.getCode()) {
                        return null;
                    } else {
                        return vehicleBase.getData();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error("根据车牌查询车辆vin信息接口异常....");
                throw new RuntimeException("车辆查询失败");
            }
        } else {
            //2:代表车辆vin号码
            VehicleBase vehicleBase = baseRemoteService.getVehicleBaseByVin(vo.getNum());
            if (vehicleBase != null) {
                if (vo.getSysUserType().intValue() == UserTypeEnum.COMPANY.getFlag()
                        && vehicleBase.getAuthorizedStatus().intValue() == AuthorizedEnum.UN_AUTHORIZED.getCode()) {
                    return null;
                } else {
                    return vehicleBase;
                }
            } else {
                return null;
            }
        }
        return null;
    }

}
